# from django.shortcuts import render
# # Create your views here.
# from rest_framework.decorators import action
# from rest_framework.response import Response
# from rest_framework.views import APIView
# from rest_framework.viewsets import ViewSet
# from rest_framework_jwt.utils import jwt_payload_handler
#
# from sadmin.serializer_login import BookInfoSerializer
#
#
# class AdminLogin(ViewSet):
#     @action(methods=["post"],detail=False)
#     def authorizations(self,request):
#         # 1、构建序列化器，传入前端浏览的参数，进行校验
#         data = request.data
#         serializer=BookInfoSerializer(data=data)
#         serializer.is_valid(raise_exception=True)
#         return Response({
#             "username": serializer.validated_data['user'].username,
#             "user_id": serializer.validated_data['user'].id,
#             "token": serializer.validated_data['token'],
#         })
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.views import APIView

from sadmin.serializersss.serializer_login import LoginSerializer


class AdminLoginView(APIView):
    @action(methods=["post"], detail=False)
    def post(self, reqeust):
        # 1、构建序列化器，传入前端浏览的参数，进行校验
        s = LoginSerializer(data=reqeust.data)
        s.is_valid(raise_exception=True)
        # 2、得到有效数据，构建响应对象
        return Response({
            "username": s.validated_data['user'].username,
            "user_id": s.validated_data['user'].id,
            "token": s.validated_data['token'],
        })
